package com.store.pet.petstore.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.store.pet.petstore.R;
import com.store.pet.petstore.activity.AboutActivity;
import com.store.pet.petstore.activity.ChangePasswordActivity;
import com.store.pet.petstore.activity.InformationActivity;
import com.store.pet.petstore.activity.LoginActivity;
import com.store.pet.petstore.activity.MyCommodityActivity;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.SharedPreferencesUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 个人Fragment
 */
public class PersonFragment extends BaseFragment {

    @BindView(R.id.information)
    TextView information;
    @BindView(R.id.changePassword)
    TextView changePassword;
    @BindView(R.id.about)
    TextView about;
    @BindView(R.id.logout)
    TextView logout;
    @BindView(R.id.commodity)
    TextView commodity;

    public static final int CHANGE_SUCCESS = 0x0003;

    public static final int ADD_COMMODITY = 0x0005;

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_person;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /**
         * 判断是否为卖家
         * 卖家才有"我的发布"功能
         */
        if(getUserRole() == 2){
            commodity.setVisibility(View.VISIBLE);
        }else {
            commodity.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.information, R.id.changePassword, R.id.about, R.id.logout, R.id.commodity})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.commodity:
                intent.setClass(getActivity(),MyCommodityActivity.class);
                startActivityForResult(intent,ADD_COMMODITY);
                break;
            case R.id.information:
                intent.setClass(getActivity(),InformationActivity.class);
                startActivity(intent);
                break;
            case R.id.changePassword:
                intent.setClass(getActivity(), ChangePasswordActivity.class);
                startActivityForResult(intent,CHANGE_SUCCESS);
                break;
            case R.id.about:
                intent.setClass(getActivity(), AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.logout:
                gotoLoginActivity();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data != null){
            if(resultCode == CHANGE_SUCCESS){
                gotoLoginActivity();
            }
        }
    }

    public void gotoLoginActivity(){
        Intent intent = new Intent();
        //清理缓存
        SharedPreferencesUtil.getInstance().removeAll();
        SharedPreferencesUtil.deleteBean(getContext(), Constant.FILE_NAME, Constant.DATA);
        intent.setClass(getActivity(), LoginActivity.class);
        startActivity(intent);
        getActivity().finish();  //退出界面
    }
}
